package model.dao;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

import controller.main.Constants;
import controller.main.Executer;

import model.dto.Location;

public class CountryDao {
	

	
	
	public static final String PROPERTY_FILE_PATH="/controller/configrations.properties";
	List<Location> locationsList=new ArrayList<Location>();
	
	
	
	
	public List<Location> getCountryLocations(String countryName) {
	
   Properties prop=new Properties();
// TODO Auto-generated constructor stub
	try {
		//prop.load(CountryDao.class.getResourceAsStream(PROPERTY_FILE_PATH));
		prop.load(new FileInputStream(Executer.getJarDirectory()+File.separator+"configrations.properties"));
		
		
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	System.out.println("dddddddd");
	}
	


	Enumeration e=prop.propertyNames();
	while (e.hasMoreElements()) {
		Location location=new Location();
		String key = (String) e.nextElement();
	      String country_location=prop.getProperty(key);
	      
	      System.out.println(country_location);
	      String[] temp;
	      temp = country_location.split(",");
			
	  	for(int i =0; i < temp.length ; i++)
	  	{
	  		//System.out.println(temp[i]);
	  		String[] temp2;
		      temp2 = temp[i].split("=");
		      
		      for(int j =0; j < temp2.length ; j++)
			  	{
			  		//System.out.println(temp2[j]);
			  		if(temp2[j].equalsIgnoreCase("country"))
			  		{
			  			location.setCountry(temp2[j+1]);
			  			System.out.println("COUNTRY==>"+location.getCountry());
			  		
			  		}
			  		else if(temp2[j].equalsIgnoreCase("ip"))
			  		{
			  			location.setIp(temp2[j+1]);
			  			System.out.println("IP==>"+location.getIp());
			  		}
			  		else if(temp2[j].equalsIgnoreCase("username"))
			  		{
			  			location.setUsername(temp2[j+1]);
			  			System.out.println("USERNAME==>"+location.getUsername());
			  		}
			  		else if(temp2[j].equalsIgnoreCase("pass"))
			  		{
			  			location.setPass(temp2[j+1]);
			  			System.out.println("PASSWORD==>"+location.getPass());
			  		}
			  		else if(temp2[j].equalsIgnoreCase("localpath"))
			  		{
			  			location.setLocalPath(temp2[j+1]);
			  			System.out.println("LOCALPATH==>"+location.getLocalPath());
			  		}
			  		else if(temp2[j].equalsIgnoreCase("remotepath"))
			  		{
			  			location.setRemotePath(temp2[j+1]);
			  			System.out.println("REMOTEPATH==>"+location.getRemotePath());
			  		}
			  		else if(temp2[j].equalsIgnoreCase("days"))
			  		{
			  			location.setDays(temp2[j+1]);
			  			System.out.println("DAYS==>"+location.getDays());
			  		}
			  		else if(temp2[j].equalsIgnoreCase("numberoffiles"))
			  		{
			  			location.setNumOfFiles(temp2[j+1]);
			  			System.out.println("NumberOfFiles==>"+location.getNumOfFiles());
			  		}
			  		else if(temp2[j].equalsIgnoreCase("filecontains"))
			  		{
			  			location.setFileContains(temp2[j+1]);
			  			System.out.println("NuamesOfFiles==>"+location.getFileContains());
			  		}
			  		else if(temp2[j].equalsIgnoreCase("fileextension"))
			  		{
			  			location.setFileExtension(temp2[j+1]);
			  			System.out.println("ExtensionOfFiles==>"+location.getFileExtension());
			  		}
			  		else if(temp2[j].equalsIgnoreCase("archivingpath")){
			  			location.setArchivingPath(temp2[j+1]);
			  		}
			  		else if(temp2[j].equalsIgnoreCase("archivingperiod")){
			  			location.setArchivingPeriod(Integer.parseInt(temp2[j+1]));
			  		}
			  	}
		     
	  	}
	  	
	  	if(countryName==Constants.ALL)
	  	{
	  		locationsList.add(location);
	  		System.out.println("iffffffffffffffff");
	  	}
	  	else
	  	{
	  		System.out.println("elseeeeeeeeeee");
	  	 if(location.getCountry().equalsIgnoreCase(countryName))
	  	 {
	  	 locationsList.add(location);
	  	 }
	  	}
	}
		return locationsList;
  }
}
